package net.zjitc.controller;

import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/9.
 */
public class UserController {
    UserService userService = new UserServiceImpl();
    @URLMapping(url = "/findAllUser")
    public String findAll(HttpServletRequest request, HttpServletResponse response){

        List<User> list = userService.findAll();
        request.setAttribute("list",list);
        return "/admin/user/list.jsp";
    }

    @URLMapping(url = "/loginUI")
    public String loginUI(HttpServletRequest request, HttpServletResponse response){
        return "/WEB-INF/user/login.jsp";
    }

    /**
     * 进入注册页面
     * @param request
     * @param response
     * @return
     */
    @URLMapping(url = "/registUI")
    public String registUI(HttpServletRequest request, HttpServletResponse response){
        return "/WEB-INF/user/regist.jsp";
    }

    /**
     * 进行用户注册
     * @param request
     * @param response
     * @return
     */
    @URLMapping(url = "/regist")
    public String regist(HttpServletRequest request, HttpServletResponse response){
       String inputCode = request.getParameter("code");
       String trueCode = (String) request.getSession().getAttribute("validatecCode");
       if (!inputCode.equalsIgnoreCase(trueCode)){
           request.setAttribute("codeMsg","验证码错误");
           return "/WEB-INF/user/regist.jsp";
       }
        User user = new User();

        try {
            BeanUtils.populate(user,request.getParameterMap());

            //
            int count = userService.save(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //登录激活提示页面
         request.setAttribute("msg","注册成功，请先激活再使用");
          return "/WEB-INF/user/msg.jsp";
    }

    @URLMapping(url = "/findByUsername")
    public String findByUsername(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");

       User user =  userService.findByUsername(username);
       if (user==null){
           response.getWriter().print(true);
       }else {
           response.getWriter().print(false);
       }
        return null;
    }

    @URLMapping(url = "/activeCode")
    public String activeCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");

        User user = userService.findByCode(code);
        if (user!=null){
            user.setCode(null);
            user.setState(1);
            int count = userService.update(user);
            request.setAttribute("msg","激活成功，请登录");
        }else {
            request.setAttribute("msg","激活失败，请先注册");
        }
       // response.getWriter().print("激活成功");
        //查询用户
        //User user = userService.findCode(code);
        //修改用户状态
       // user.setState(1);
        //更新用户
        //userService.update(user);
        return "/WEB-INF/user/msg.jsp";
    }
}
